var app = getApp();

Page({
  data: {
    id: '',
    goods: {},
    show_attr_picker: !1,
    form: {
      number: 1
    },
    tab_detail: "active",
    tab_comment: "",
    comment_list: [],
    comment_count: {
      score_all: 0,
      score_3: 0,
      score_2: 0,
      score_1: 0,
      evaluateList: [],
      cp: null,
      zp: null,
      hp: null
    },
    autoplay: !1,
    hide: "hide",
    show: !1,
    x: wx.getSystemInfoSync().windowWidth,
    y: wx.getSystemInfoSync().windowHeight - 20,
    xdggid: "",
    pjindex: 2,
    pagenum: 1,
    storelist: [],
    mygd: !1,
    jzgd: !0,
    page: 1,
    limit: 10,
    types: 1,
    id: '',
    category: [],
    parductImageList: [{
      url: null
    }],
    guige: [],
    firstValue1: '',
    secondValue1: '',
    thirdValue1: '',
    price: "",
    kucun: '',
    shopData: ''
  },
  onLoad: function(t) {
    this.setData({
      id: t.id,
    })

  },
  getguige() {
    var that = this;
    app.getData({
      url: app.api.getGuige() + `?id=${JSON.parse(that.data.id)}&firstValue=${that.data.firstValue1}&secondValue=${that.data.secondValue1}&thirdValue=${that.data.thirdValue1}`,
      method: 'POST',
      ok: function(res) {
        console.log(res)
        if (res.statusCode == 200 && res.errMsg == 'request:ok') {
          if (res.data.code == 1) {
            that.setData({
              guige: res.data.data,
              price: res.data.data.price
            })
          } else {
            console.log(1111)
            that.setData({
              guige: ''
            })
          }
        }
      }
    })
  },
  onShow: function() {
    // this.reLoad();
    var that = this
    // 获取商品详情
    app.getData({
        url: app.api.getSpxqData(),
        method: 'GET',
        data: {
          page: that.data.page,
          limit: that.data.limit,
          type: that.data.types,
          id: that.data.id
        },
        ok: function(res) {
          console.log(res)
          if (res.statusCode == 200 && res.errMsg == 'request:ok') {
            if (res.data.code == 1) {
              var reg = new RegExp('<img', "g")
              console.log(that.data.detailImg)
              that.parductImageList = JSON.parse(res.data.data.hyParductManageBean.parductImage)
              that.setData({
                detailImg: res.data.data.hyParductManageBean.parductDetails.replace(reg, '<img style="max-width:100%;height:auto" '),
                category: res.data.data,
                parductImageList: that.parductImageList,
                price: res.data.data.hyParductManageBean.parductSalesPrice
              })
              console.log(that.parductImageList)
            } else {
              wx.showToast({
                title: res.data.message,
              })
            }
          }
        }

      }),
      this.getCommentList(2),
      this.getguige(),
      this.setData({
        shopData: wx.getStorageSync('shopData')
      })
  },
  //获取库存
  getKucun: function() {
    console.log(this.data.guige.stock, this.data.category.hyParductManageBean.parductStock);
    (this.data.guige.stock == null || this.data.guige.stock == '') ? this.setData({
      kucun: this.data.category.hyParductManageBean.parductStock
    }): this.setData({
      kucun: this.data.guige.stock
    })
  },
  // 获取商品评价
  getCommentList: function(t) {
    console.log(t)
    console.log(t, this.data.pagenum)
    var that = this;
    app.getData({
      url: app.api.productEvaluate(),
      method: "GET",
      data: {
        parductId: that.data.id,
        commentGrade: t
      },
      ok: function(res) {
        console.log(res)
        var hp = [],
          zp = [],
          cp = []
        if (res.statusCode == 200 && res.errMsg == 'request:ok') {
          if (res.data.code == 1) {
            if (res.data.data == null || res.data.data == '') {
              that.setData({
                evaluateList: res.data.data,
              })
            } else {
              for (var evaluateList = res.data.data, qbpj = res.data.data, i = 0; i < evaluateList.length; i++) {
                if (evaluateList[i].commentGrade == 0) {
                  cp.push(evaluateList[i])
                } else if (evaluateList[i].commentGrade == 1) {
                  zp.push(evaluateList[i])
                } else if (evaluateList[i].commentGrade == 2) {
                  hp.push(evaluateList[i])
                }
              }
              console.log(cp, zp, hp)
              that.setData({
                evaluateList: res.data.data,
                cp: cp,
                zp: zp,
                hp: hp
              })
            }

          }
        }
      }
    })
    // var o = this;
    // console.log(t, o.data.pagenum), app.util.request({
    //   url: "entry/wxapp/PjList",
    //   cachetime: "0",
    //   data: {
    //     good_id: o.data.id,
    //     score: t,
    //     page: o.data.pagenum,
    //     pagesize: 10
    //   },
    //   success: function (t) {
    //     console.log("分页返回数据", t.data), t.data.length < 10 ? o.setData({
    //       mygd: !0,
    //       jzgd: !0
    //     }) : o.setData({
    //       jzgd: !0,
    //       pagenum: o.data.pagenum + 1
    //     });
    //     var a = o.data.storelist;
    //     for (var e in a = function (t) {
    //       for (var a = [], e = 0; e < t.length; e++) -1 == a.indexOf(t[e]) && a.push(t[e]);
    //       return a;
    //     }(a = a.concat(t.data)), console.log(a), t.data) t.data[e].img = t.data[e].img.split(",");
    //     o.setData({
    //       comment_list: a,
    //       storelist: a
    //     });
    //   }
    // });
  },
  reLoad: function() {
    var a = this,
      t = wx.getStorageSync("mdid"),
      e = wx.getStorageSync("UserData").id;
    app.util.request({
      url: "entry/wxapp/MyCarNum",
      cachetime: "0",
      data: {
        store_id: t,
        user_id: e
      },
      success: function(t) {
        console.log(t), a.setData({
          gwccd: t.data
        });
      }
    });
  },
  djpj: function(t) {
    console.log(t.currentTarget.dataset.pjindex), this.setData({
      pjindex: t.currentTarget.dataset.pjindex,
    }), this.getCommentList(t.currentTarget.dataset.pjindex);
  },
  onGoodsImageClick: function(t) {
    var a = this,
      e = [],
      o = t.currentTarget.dataset.index;
    for (var i in a.data.goodinfo.img) e.push(a.data.url + a.data.goodinfo.img[i]);
    wx.previewImage({
      urls: e,
      current: e[o]
    });
  },
  numberSub: function() {
    var t = this.data.form.number;
    if (t <= 1) return !0;
    t--, this.setData({
      form: {
        number: t
      }
    });
  },
  numberAdd: function() {
    var t = this.data.form.number;
    t++, this.setData({
      form: {
        number: t
      }
    });
  },
  numberBlur: function(t) {
    console.log(t)
    var a = t.detail.value;
    a = parseInt(a), isNaN(a) && (a = 1), a <= 0 && (a = 1), this.setData({
      form: {
        number: a
      }
    });
  },
  addCart: function() {
    this.submit("ADD_CART");
  },
  buyNow: function() {
    this.submit("BUY_NOW");
  },
  submit: function(t) {
    this.getKucun()
    var a = this
    console.log(a)
    if (!a.data.show_attr_picker) return a.setData({
      show_attr_picker: !0
    }), !0;
    if (console.log(a.data.form.number, this.data.kucun), a.data.form.number > this.data.kucun) return wx.showToast({
      title: "商品库存不足，请选择其它规格或数量",
      image: "../../../../image/jg.png"
    }), !0;
    console.log(this.data.guige)
    if ("BUY_NOW" == t && a.data.show_attr_picker == !0) {

      if (this.data.guige == null || this.data.guige == '') {
        console.log(123)
        wx.setStorageSync("cart_list", {
          good_id: this.data.category.hyParductManageBean.id,
          name: this.data.category.hyParductManageBean.parductName,
          money: this.data.category.hyParductManageBean.parductSalesPrice,
          companyid: this.data.category.hyParductManageBean.companyId,
          img: this.data.category.hyParductManageBean.parductSimpleImage,
          numbers: this.data.form.number,
          guigeId: this.data.guige.id,
          oldPrice: this.data.category.hyParductManageBean.parductVirtualSales,
          guige: this.data.guige
        })
        wx.redirectTo({
          url: "../../../package/pages/shopDetail/address"
        })
      } else if ((this.data.category.listFirstName != null && this.data.firstValue1) || (!(this.data.category.listSecondName == null) && this.data.secondValue1) || (this.data.category.listThirdName != null && this.data.thirdValue1)) {
        wx.setStorageSync("cart_list", {
            good_id: this.data.category.hyParductManageBean.id,
            name: this.data.category.hyParductManageBean.parductName,
            money: this.data.guige.price,
            companyid: this.data.category.hyParductManageBean.companyId,
            img: this.data.category.hyParductManageBean.parductSimpleImage,
            numbers: this.data.form.number,
            guigeId: this.data.guige.id,
            oldPrice: this.data.category.hyParductManageBean.parductVirtualSales,
            guige: this.data.guige
          }),
          wx.redirectTo({
            url: "../../../package/pages/shopDetail/address"
          })
      } else {
        wx.showToast({
          title: '请选择规格~',
        })
        console.log(123)
      }
    }
  },
  hideAttrPicker: function() {
    this.setData({
      show_attr_picker: !1
    });
  },
  showAttrPicker: function() {
    this.setData({
      show_attr_picker: !0
    });
  },
  // 选择规格
  // attrClick: function(t) {
  //   console.log(t)
  //   var e = this,
  //     a = t.target.dataset.groupId,
  //     o = t.target.dataset.id,
  //     i = e.data.goodinfo;
  //   for (var s in console.log(a, o, i), i.spec)
  //     if (i.spec[s].spec_id == a)
  //       for (var n in i.spec[s].spec_val) i.spec[s].spec_val[n].spec_val_id == o ? i.spec[s].spec_val[n].checked = !0 : i.spec[s].spec_val[n].checked = !1;
  //   e.setData({
  //     goodinfo: i
  //   });
  //   var d = [],
  //     r = !0;
  //   for (var s in i.spec) {
  //     var c = !1;
  //     for (var n in i.spec[s].spec_val)
  //       if (i.spec[s].spec_val[n].checked) {
  //         d.push(i.spec[s].spec_val[n].spec_val_name), c = !0;
  //         break;
  //       }
  //     if (!c) {
  //       r = !1;
  //       break;
  //     }
  //   }
  //   console.log(e.data.goodinfo.id, d, d.toString()), r && (wx.showLoading({
  //     title: "正在加载",
  //     mask: !0
  //   }), app.util.request({
  //     url: "entry/wxapp/GgZh",
  //     cachetime: "0",
  //     data: {
  //       combination: d.toString(),
  //       good_id: e.data.goodinfo.id
  //     },
  //     success: function(t) {
  //       console.log(t);
  //       var a = e.data.goodinfo;
  //       a.money = t.data.money, a.inventory = t.data.number, e.setData({
  //         goodinfo: a,
  //         xdggid: t.data.id
  //       });
  //     }
  //   }));
  // },
  radioChange1: function(e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    this.setData({
      firstValue1: e.detail.value,
    })
    this.getguige()
  },
  radioChange2: function(e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    this.setData({
      secondValue1: e.detail.value
    })
    this.getguige()
  },
  radioChange3: function(e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    this.setData({
      thirdValue1: e.detail.value
    })
    this.getguige()
  },
  favoriteAdd: function() {
    var e = this;
    o.request({
      url: t.user.favorite_add,
      method: "post",
      data: {
        goods_id: e.data.goods.id
      },
      success: function(t) {
        if (0 == t.code) {
          var a = e.data.goods;
          a.is_favorite = 1, e.setData({
            goods: a
          });
        }
      }
    });
  },
  favoriteRemove: function() {
    var e = this;
    o.request({
      url: t.user.favorite_remove,
      method: "post",
      data: {
        goods_id: e.data.goods.id
      },
      success: function(t) {
        if (0 == t.code) {
          var a = e.data.goods;
          a.is_favorite = 0, e.setData({
            goods: a
          });
        }
      }
    });
  },
  tabSwitch: function(t) {
    console.log(t.currentTarget.dataset.tab)
    "detail" == t.currentTarget.dataset.tab ? this.setData({
      tab_detail: "active",
      tab_comment: ""
    }) : this.setData({
      tab_detail: "",
      tab_comment: "active"
    });
  },
  commentPicView: function(t) {
    console.log(t);
    var a = this.data.comment_list,
      e = [],
      o = t.currentTarget.dataset.index,
      i = t.currentTarget.dataset.picindex,
      s = t.currentTarget.dataset.id;
    if (console.log(o, i, s), s == a[o].id) {
      var n = a[o].img;
      for (var d in n) e.push(this.data.url + n[d]);
      wx.previewImage({
        current: this.data.url + n[i],
        urls: e
      });
    }
  },
  onReady: function() {},
  onHide: function() {},
  onUnload: function() {},
  onPullDownRefresh: function() {},
  onReachBottom: function() {
    // var t = this.data.pjindex;
    // console.log("上拉加载", this.data.pagenum, t), !this.data.mygd && this.data.jzgd && (this.setData({
    //   jzgd: !1
    // }), this.getCommentList(t));
  },
  onShareAppMessage: function() {
    // var t = wx.getStorageSync("UserData").nickname;
    // wx.getStorageSync("user_info");
    // return {
    //   path: "/zh_vip/pages/psdj/spxq?id=" + this.data.id,
    //   success: function(t) {
    //     console.log(t);
    //   },
    //   title: t + "邀请你来看" + this.data.goodinfo.name,
    //   imageUrl: this.data.url + this.data.goodinfo.img[0]
    // };
    this.shareModalClose()
  },
  play: function(t) {
    var a = t.target.dataset.url;
    this.setData({
      url: a,
      hide: "",
      show: !0
    }), wx.createVideoContext("video").play();
  },
  close: function(t) {
    if ("video" == t.target.id) return !0;
    this.setData({
      hide: "hide",
      show: !1
    }), wx.createVideoContext("video").pause();
  },
  hide: function(t) {
    0 == t.detail.current ? this.setData({
      img_hide: ""
    }) : this.setData({
      img_hide: "hide"
    });
  },
  showShareModal: function() {
    this.setData({
      share_modal_active: "active",
      no_scroll: !0
    });
  },
  shareModalClose: function() {
    this.setData({
      share_modal_active: "",
      no_scroll: !1
    });
  },
  getGoodsQrcode: function() {
    var a = this;
    if (a.setData({
        goods_qrcode_active: "active",
        share_modal_active: ""
      }), a.data.goods_qrcode) return !0;
    o.request({
      url: t.default.goods_qrcode,
      data: {
        goods_id: a.data.id
      },
      success: function(t) {
        0 == t.code && a.setData({
          goods_qrcode: t.data.pic_url
        }), 1 == t.code && (a.goodsQrcodeClose(), wx.showModal({
          title: "提示",
          content: t.msg,
          showCancel: !1,
          success: function(t) {
            t.confirm;
          }
        }));
      }
    });
  },
  goodsQrcodeClose: function() {
    this.setData({
      goods_qrcode_active: "",
      no_scroll: !1
    });
  },
  saveGoodsQrcode: function() {
    var a = this;
    wx.saveImageToPhotosAlbum ? (wx.showLoading({
      title: "正在保存图片",
      mask: !1
    }), wx.downloadFile({
      url: a.data.goods_qrcode,
      success: function(t) {
        wx.showLoading({
          title: "正在保存图片",
          mask: !1
        }), wx.saveImageToPhotosAlbum({
          filePath: t.tempFilePath,
          success: function() {
            wx.showModal({
              title: "提示",
              content: "商品海报保存成功",
              showCancel: !1
            });
          },
          fail: function(t) {
            wx.showModal({
              title: "图片保存失败",
              content: t.errMsg,
              showCancel: !1
            });
          },
          complete: function(t) {
            console.log(t), wx.hideLoading();
          }
        });
      },
      fail: function(t) {
        wx.showModal({
          title: "图片下载失败",
          content: t.errMsg + ";" + a.data.goods_qrcode,
          showCancel: !1
        });
      },
      complete: function(t) {
        console.log(t), wx.hideLoading();
      }
    })) : wx.showModal({
      title: "提示",
      content: "当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。",
      showCancel: !1
    });
  },
  goodsQrcodeClick: function(t) {
    var a = t.currentTarget.dataset.src;
    wx.previewImage({
      urls: [a]
    });
  },
  closeCouponBox: function(t) {
    this.setData({
      get_coupon_list: ""
    });
  },
  to_dial: function(t) {
    var a = this.data.store.contact_tel;
    wx.makePhoneCall({
      phoneNumber: a
    });
  },
  //打电话
  callPhone: function(res) {
    console.log(res)
    wx.makePhoneCall({
      phoneNumber: res.currentTarget.dataset.phone
    })
  },
  //海报
  poster:function(t){
    console.log(t)
    var shopId = t.currentTarget.dataset.id
    wx.navigateTo({
      url: 'poster?id='+shopId,
    })
  }
});